/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.event.*;
import org.w3c.dom.*;

public class java1409 extends JPanel implements ActionListener{
  private Style estiloRojo,estiloVerde,estiloAzul;
  private JTextPane texto;
  StyleContext sc = new StyleContext();
  DefaultStyledDocument doc = new DefaultStyledDocument(sc);
  MutableAttributeSet standard = new SimpleAttributeSet();


  public java1409() {
    setLayout( new BorderLayout() );
    add( creaMenu(),BorderLayout.NORTH );
    //final JTextPane texto = creaEditor();
    texto=new JTextPane(doc);
      StyleConstants.setForeground(standard, Color.black);
     doc.setCharacterAttributes(0,texto.getText().length(),standard, false);
    final MutableAttributeSet characters = new SimpleAttributeSet();
    StyleConstants.setForeground(characters, Color.green.darker());
    texto.addKeyListener(new KeyAdapter(){
    public void keyPressed(KeyEvent e) {
      /*System.out.println("Key press detected:" + e.getKeyChar());
      System.out.println("code key : "+ e.getKeyCode());
      SimpleAttributeSet atributo = new SimpleAttributeSet();
      StyleConstants.setFontSize(atributo,14);
      StyleConstants.setForeground(atributo,Color.blue);
      SimpleAttributeSet atributo2 = new SimpleAttributeSet();
      StyleConstants.setFontSize(atributo2,12);
      StyleConstants.setForeground(atributo2,Color.black);
      String soriginal=texto.getText();
      System.out.println(texto.getText());
      int post=0;
      post=post=soriginal.indexOf("entero");
      texto.getStyledDocument().setCharacterAttributes(0,soriginal.length(),atributo2,false);
      texto.setCharacterAttributes(atributo2,false);
      while (post > -1) {
         texto.getStyledDocument().setCharacterAttributes(post,post+"entero".length(),atributo,false);
         texto.setCharacterAttributes(atributo,false);
         texto.getStyledDocument().setCharacterAttributes(post+"entero".length(),post+"entero".length()+1,atributo2,false);
         texto.setCharacterAttributes(atributo2,false);
         post=soriginal.indexOf("entero",post+1);
      }*/
     }
     public void keyReleased(KeyEvent e) {
       System.out.println("Key release detected:" + e.getKeyChar());
       System.out.println("code key : "+ e.getKeyCode());
       SimpleAttributeSet atributo = new SimpleAttributeSet();
       StyleConstants.setFontSize(atributo,14);
       StyleConstants.setForeground(atributo,Color.blue);
       SimpleAttributeSet atributo2 = new SimpleAttributeSet();
       StyleConstants.setFontSize(atributo2,12);
       StyleConstants.setForeground(atributo2,Color.black);
       String soriginal=texto.getText();
       System.out.println(texto.getText());
       int post=0;
       post=post=soriginal.indexOf("entero");
       texto.getStyledDocument().setCharacterAttributes(0,soriginal.length(),atributo2,false);
       texto.setCharacterAttributes(atributo2,false);
       while (post > -1) {
         texto.getStyledDocument().setCharacterAttributes(post,post+"entero".length(),atributo,false);
         texto.setCharacterAttributes(atributo,false);
         texto.getStyledDocument().setCharacterAttributes(post+"entero".length(),post+"entero".length()+1,atributo2,false);
         texto.setCharacterAttributes(atributo2,false);
         post=soriginal.indexOf("entero",post+1);
       }
       System.out.println(texto.getText());
     }
    });
    
    //texto.setCharacterAttributes( estiloRojo, false);
    //doc.addDocumentListener(new MyDocumentListener(texto,doc,characters,estiloRojo));
    add( texto,BorderLayout.CENTER );      
  }
  
  private JMenuBar creaMenu() {
    JMenuBar menu = new JMenuBar();
    JMenu estilo = new JMenu( "Estilo" );
    menu.add( estilo );
    
    JMenuItem mi = new JMenuItem( "Rojo" );
    estilo.add( mi );
    mi.addActionListener( this );
    
    mi = new JMenuItem( "Verde" );
    estilo.add( mi );
    mi.addActionListener( this );
    
    mi = new JMenuItem( "Azul" );
    estilo.add( mi );
    mi.addActionListener( this );
    
    return( menu );
  }
    
  public void actionPerformed( ActionEvent evt ) {
    Style estilo = null;
    String color = (String)evt.getActionCommand();
    
    if( color.equals( "Rojo" ) ) {
      estilo = estiloRojo;
    } else if( color.equals( "Azul" ) ) {
      estilo = estiloAzul;
    } else if( color.equals( "Verde" ) ) {
      estilo = estiloVerde;
    }
    texto.setCharacterAttributes( estilo,false );
  }
  
  
  private JTextPane creaEditor() {
    StyleContext sc = creaEstilos();
    DefaultStyledDocument doc = new DefaultStyledDocument( sc );
    
    return( texto = new JTextPane( doc ) );
  }
  
  private StyleContext creaEstilos() {
    StyleContext sc = new StyleContext();
    
    estiloRojo = sc.addStyle( null,null );
    StyleConstants.setForeground( estiloRojo,Color.red );
    
    estiloVerde = sc.addStyle( null,null );
    StyleConstants.setForeground( estiloVerde,Color.green );
    StyleConstants.setFontSize( estiloVerde,24 );
    
    estiloAzul = sc.addStyle( null,null );
    StyleConstants.setForeground( estiloAzul,Color.blue );
    
    return( sc );
  }
  private void operacion(){
            System.out.print("presiono");
            estiloRojo = sc.addStyle( null,null );
            StyleConstants.setForeground( estiloRojo,Color.red );
            String sTexto=texto.toString();
            int contador=0;
            while (sTexto.indexOf("int") > -1) {
            if(sTexto.indexOf("int") > -1)
            {
             doc.setCharacterAttributes(sTexto.indexOf("int"), sTexto.indexOf("int")+"int".length(), estiloRojo, false);   
            }
            sTexto = sTexto.substring(sTexto.indexOf("int")+"int".length(),sTexto.length());
            }
  }
  
  public static void main( String argv[] ) {
    JFrame ventana = new JFrame( "Tutorial de Java, Swing" );
    
    ventana.addWindowListener( new WindowAdapter() {
      public void windowClosing( WindowEvent evt ){
	System.exit( 0 );
      }
    } );
    ventana.getContentPane().add( new java1409(),BorderLayout.CENTER );
    ventana.setSize( 300,180 );
    
    ventana.setVisible( true );
  }
}
